grammar EquationGrammar;

options {
  language = Java;
  output=AST;
  ASTLabelType=CommonTree;
  backtrack=true;
}

tokens { 
	FUNCTION;
	CONDITION;
	ATOMIC_RULE;
	MATH;
	   }

@header {
  package com.sri.ai.lpitools.latex.server;
}

@lexer::header {
  package com.sri.ai.lpitools.latex.server;
}

converter
	: clause EOF
	;
	
clause
  : IF assignment THEN a=clause ELSE b=clause -> ^( CONDITION assignment $a $b )
  | CR '(' assignment ',' a=clause ',' b=clause ')' -> ^( CONDITION assignment $a $b )
  | AR '(' c=clause ',' v=clause ')' -> ^( ATOMIC_RULE $c $v )
  | expression
  ;

term
	:	IDENT
	|	'('^ clause ')'
	|	'|'^ clause '|'
	|	number
	;

function 
	: 	IDENT '(' clause ')' -> ^( FUNCTION IDENT clause )
	|	term
	;
	
assignment
	:	function (('='^ | '!='^) function)*
	;
	
exponent
	:	function ('^'^ function)*
	;

mult
	:	exponent (('*'^ | '/'^ | '%'^) exponent)*
	;
	
expression
	:	mult (('+'^ | '-'^  | 'and'^) mult)*
	;
	
number
	:	INTEGER
	|	FLOAT
	;
  
fragment LETTER : ('a'..'z' | 'A'..'Z') ;
fragment DIGIT : '0'..'9';
IF : 'if' | 'If';
THEN : 'then' | 'Then';
ELSE : 'else' | 'Else';
INTEGER : DIGIT+ ;
FLOAT : INTEGER '.' INTEGER;
IDENT : LETTER (LETTER | DIGIT)*;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;};

CR : '\'conditional rule\'';
AR : '\'atomic rule\'';